﻿$(document).ready(function () {
    $("#btnSave:first").click(function (e) {
        e.preventDefault();
        var lblMessage = $("#lblMessage:first").text("");
        $(".required:visible").each(function () {
            $(this).removeClass("ui-state-error");
            if ($(this).val() == "") {
                lblMessage.text("É necessário preencher os campos obrigatórios.");
                $(this).addClass("ui-state-error");
            }
        });

        if (lblMessage.text() == "") {
            $.ajax({
                dataType: "json",
                type: "POST",
                url: "/Vehicle/Save/" + $("#hdfIdVehicle").val(),
                data: {
                    number_plate: $("#txtNumberPlate").val(),
                    year: $("#txtYear").val(),
                    mark: $("#txtMark").val(),
                    model: $("#txtModel").val(),
                    description: $("#txtDescription").val()
                },
                success: function (json) {
                    document.location.href = "/Vehicle/Index";
                },
                error: function (json) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        };
    });

    $("#txtYear:first").keydown(function (event) {
        if (event.shiftKey) {
            event.preventDefault();
        }
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) {
        }
        else {
            if (event.keyCode < 95) {
                if (event.keyCode < 48 || event.keyCode > 57) {
                    event.preventDefault();
                }
            }
            else {
                if (event.keyCode < 96 || event.keyCode > 105) {
                    event.preventDefault();
                }
            }
        }
    });
    $("#txtNumberPlate:first").focus();
});